package com.cjh.wechat.core.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cjh.wechat.commom.pojo.Group;
import com.cjh.wechat.commom.pojo.Team;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.pojo.UserAndGroup;
import com.cjh.wechat.commom.type.UserStatus;
import com.cjh.wechat.commom.vo.FriendVo;
import com.cjh.wechat.commom.vo.GroupVo;
import com.cjh.wechat.commom.vo.InitDataVo;
import com.cjh.wechat.commom.vo.MineVo;
import com.cjh.wechat.commom.vo.ResponseVo;
import com.cjh.wechat.core.dao.GroupDao;
import com.cjh.wechat.core.dao.TeamDao;
import com.cjh.wechat.core.dao.UserDao;
import com.cjh.wechat.core.service.FriendService;
import com.cjh.wechat.core.service.InitService;
import com.cjh.wechat.core.service.UserAndGroupService;

@Service
public class InitServiceImpl implements InitService
{
    @Resource
    private UserDao             userDao;
    @Resource
    private FriendService       friendService;
    @Resource
    private GroupDao            groupDao;
    @Resource
    private TeamDao             teamDao;
    @Resource
    private UserAndGroupService userAndGroupService;
    
    @Override
    public ResponseVo getInitVo(String id)
    {
        User user = userDao.findById(id);
        List<Team> teams = teamDao.findByUid(id);
        List<UserAndGroup> userAndGroups = userAndGroupService.findByUid(id);
        if (user == null)
        {
            return buildErrorVo();
        }
        else
        {
            user.setStatus(UserStatus.ONLINE.value);
            return buildInitVo(user, teams, userAndGroups);
        }
    }
    
    private ResponseVo buildErrorVo()
    {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(1);
        responseVo.setMsg("没有找到这个账号对应的信息");
        return responseVo;
    }
    
    private ResponseVo buildInitVo(User user, List<Team> teams, List<UserAndGroup> userAndGroups)
    {
        ResponseVo responseVo = new ResponseVo();
        InitDataVo initDataVo = new InitDataVo();
        initDataVo.setMine(changeToMineVo(user));
        initDataVo.setFriend(changeToFriendVo(teams));
        initDataVo.setGroup(changeToGroupVo(userAndGroups));
        responseVo.setCode(0);
        responseVo.setMsg("");
        responseVo.setData(initDataVo);
        return responseVo;
    }
    
    private MineVo changeToMineVo(User user)
    {
        MineVo mineVo = new MineVo();
        mineVo.setId(user.getId());
        mineVo.setUsername(user.getUsername());
        mineVo.setStatus(user.getStatus());
        mineVo.setSign(user.getSign());
        mineVo.setAvatar(user.getAvatar());
        return mineVo;
    }
    
    private List<FriendVo> changeToFriendVo(List<Team> teams)
    {
        List<FriendVo> friendVos = new ArrayList<FriendVo>();
        for (Team team : teams)
        {
            FriendVo friendVo = new FriendVo();
            friendVo.setGroupname(team.getName());
            friendVo.setId(team.getNo());
            friendVo.setList(friendService.getByTno(team.getUid(), team.getNo()));
            friendVos.add(friendVo);
        }
        return friendVos;
    }
    
    private List<GroupVo> changeToGroupVo(List<UserAndGroup> userAndGroups)
    {
        List<GroupVo> groupVos = new ArrayList<GroupVo>();
        for (UserAndGroup userAndGroup : userAndGroups)
        {
            Group group = groupDao.findById(userAndGroup.getGid());
            GroupVo groupVo = buildGroupVo(group);
            groupVos.add(groupVo);
        }
        return groupVos;
    }
    
    private GroupVo buildGroupVo(Group group)
    {
        GroupVo groupVo = new GroupVo();
        groupVo.setAvatar(group.getAvatar());
        groupVo.setGroupname(group.getName());
        groupVo.setId(group.getId());
        return groupVo;
    }
}
